using System;
using System.Collections.Generic;
using System.Text;
using SchEdu.Framework.DataModel;
using SchEdu.Framework.Exceptions;

namespace SchEdu.Framework.Validators
{

    public class DependencyPerOrderValidator : Validator
    {
        int maxDependencyPerOrder = -1;

        public int MaxDependencyPerOrder
        {
            get { return maxDependencyPerOrder; }
            set { maxDependencyPerOrder = value; }
        }

        int minDependencyPerOrder = -1;

        public int MinDependencyPerOrder
        {
            get { return minDependencyPerOrder; }
            set { minDependencyPerOrder = value; }
        }

        public DependencyPerOrderValidator(SchEduData schEduData, int minDependencyPerOrder, int maxDependencyPerOrder)
            : base(schEduData)
        {
            this.minDependencyPerOrder = minDependencyPerOrder;
            this.maxDependencyPerOrder = maxDependencyPerOrder;
        }
        #region IValidator Members

        public override void Validate()
        {
            foreach (SchEduDataSet.OrderRow order in SchEduData.Order)
            {
                int count = order.GetDependencyRowsByFK_Order_Dependency_Predecessor().Length;
                if (minDependencyPerOrder != -1 && count < minDependencyPerOrder )
                {
                    throw new ValidatorException(string.Format(Resources.MinDependencyPerOrder, order.OrderNo, minDependencyPerOrder));
                }
                if (maxDependencyPerOrder != -1 && count > maxDependencyPerOrder)
                {
                    throw new ValidatorException(string.Format(Resources.MaxDependencyPerOrder, order.OrderNo, maxDependencyPerOrder));
                }
            }
        }

        #endregion
    }
}
